<?php

namespace Biz\model\salesRebate;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\IdParse;

class SalesWithdrawalRecord extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_sales_rebate';

    const _DATABASE_ = 'biz_sales_rebate';

    const _TABLE_NAME = 'sales_withdrawal_record';

    const TYPE_PERSONAL = 1;//个人账户
    const TYPE_COMPANY = 2;//公司账户

    const STATUS_NORMAL = 0;//未入账，待确认发票
    const STATUS_ACCOUNTING = 1;//入账中，已收发票
    const STATUS_ACCOUNTED = 2;//已入账
    const STATUS_INVALID = 3;//已失效

    const STATUS_DELETE = 9;

    const TYPE_INVOICE_NORMAL = 1;//发票类型（普票）
    const TYPE_INVOICE_SPECIAL = 2;//发票类型（专票）

    public static $personStatus = [
        self::STATUS_NORMAL => '待财务转账',
        self::STATUS_ACCOUNTED => '已转账',
        self::STATUS_INVALID => '已取消',
    ];

    public static $companyStatus = [
        self::STATUS_NORMAL => '待确认发票',
        self::STATUS_ACCOUNTING => '待财务转账',
        self::STATUS_ACCOUNTED => '已转账',
        self::STATUS_INVALID => '已取消',
    ];

    public static function getAllByWhere($files = '*', $where = [])
    {
        $data = self::select($files, $where);
        return $data;
    }

    public static function getAllCount($where)
    {
        return self::count('*', $where);
    }

    public static function getOneByWhere($files = '*', $where = [])
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function addData($data)
    {
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function updateData($data, $where)
    {
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function createWithdrawalCode($uid, $type)
    {
        $prefix = '';
        $code = '';

        $count = self::count('*', ['uid' => $uid, 'type' => $type]);
        $num = $count + 1;

        if ($type == self::TYPE_COMPANY) {
            $prefix = 'TC';
        }
        if ($type == self::TYPE_PERSONAL) {
            $prefix = 'TP';
        }
        $hashUid = IdParse::hashUid($uid);
        if ($prefix && $hashUid) {
            $code = $prefix . $hashUid . str_pad($num, 6, '0', STR_PAD_LEFT);
        }
        return $code;
    }

}